package com.hnhcyy.goods.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.hnhcyy.b2b.common.util.RedisUtil;
import com.hnhcyy.b2b.promotion.client.GiveInfoClient;
import com.hnhcyy.b2b.promotion.pojo.Give;
import com.hnhcyy.goods.mapper.GoodsMapper;
import com.hnhcyy.goods.mapper.GoodsPreMapper;
import com.hnhcyy.goods.mapper.SpitemRelationMapper;
import com.hnhcyy.goods.mapper.SpkfkDescMapper;
import com.hnhcyy.goods.mapper.SpkfklackMapper;
import com.hnhcyy.goods.pojo.Goods;
import com.hnhcyy.goods.pojo.GoodsPre;
import com.hnhcyy.goods.pojo.GoodsQueryParam;
import com.hnhcyy.goods.pojo.SpitemRelation;
import com.hnhcyy.goods.pojo.SpkfkDesc;
import com.hnhcyy.goods.pojo.Spkfklack;
import com.hnhcyy.goods.service.GoodsService;


@Service
public class GoodsServiceImpl implements GoodsService{

	@Autowired 
	private GoodsMapper goodsMapper;
	@Autowired
	private RedisUtil redisUtil;
	@Autowired
	private SpkfkDescMapper spkfkDescMapper;
	@Autowired
	private SpitemRelationMapper spitemRelationMapper;
	@Autowired
	private GoodsPreMapper goodsPreMapper;
	@Autowired
	private SpkfklackMapper spkfklackMapper;
	@Value("${redis.key.goods}")
	private String REDIS_KEY_GOODS;
	@Autowired
	private GiveInfoClient giveInfoClient;


	@Override
	public List<Goods> getListByQueryParam(GoodsQueryParam queryParam) {
		/*if(ObjectUtils.nullSafeEquals(queryParam.getAppointGoods(), true)) {
			// 老客户只买指定商品
			return goodsMapper.selectListAppoint(queryParam.getDwbh());
		}else {
			// 普通控销
			return goodsMapper.selectListByQueryParam(queryParam);
		}*/
		List<Goods> goodsList = goodsMapper.selectListByQueryParam(queryParam);
		// 缺货商品添加预计到货信息
		goodsList.stream().map(e -> {
			if(e.getSpBal() == null || 
					(e.getSpBal() != null && StringUtils.isBlank(e.getSpBal().getPihao()))) {
				// 如果缺货，查询预计到货信息（日期，缺货原因...）
				Spkfklack spkfklack = spkfklackMapper.selectById(e.getSpid());
				if(spkfklack != null) {
					e.setSpkfklack(spkfklack);
				}
			}
			return e;
		}).collect(Collectors.toList());
		return goodsList;
	}

	@Override
	public Goods getItemById(String spid, String sptm, int sjid) {
		/*String key = (StringUtils.isNotBlank(spid) ? spid : sptm) + "-" + sjid;
		key = REDIS_KEY_GOODS + ":" + key + ":BASE";
		try {
			// 从缓存获取
			if(redisUtil.exists(key)) {
				Goods goods = (Goods) redisUtil.get(key);
				return goods;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}*/
		Goods goods = goodsMapper.selectItemMoreById(spid, sptm, sjid);
		/*try {
			// 写入缓存
			if(goods != null) {
				redisUtil.set(key, goods, 36000);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}*/
		if(goods == null) {
			return null;
		}
		if(goods.getSpBal() == null 
				|| (goods.getSpBal() != null && StringUtils.isBlank(goods.getSpBal().getPihao()))) {
			// 如果缺货，查询预计到货信息（日期，缺货原因等）
			Spkfklack spkfklack = spkfklackMapper.selectById(goods.getSpid());
			if(spkfklack != null) {
				goods.setSpkfklack(spkfklack);
			}
		}
		// 调用促销中心，获取该商品的促销信息
		Give give = giveInfoClient.goodsPromtionInfo(sjid, goods.getSpid());
		/*if(give != null) {
			goods.setGive(give);
		}*/
		return goods;
	}

	@Override
	public Goods getItemById(String spid) {
		String key = REDIS_KEY_GOODS + ":" + spid + ":LITTLE";
		try {
			// 从缓存获取
			if(redisUtil.exists(key)) {
				Goods goods = (Goods) redisUtil.get(key);
				return goods;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		Goods goods = goodsMapper.selectItemLittleById(spid);
		try {
			// 写入缓存
			if(goods != null) {
				redisUtil.set(key, goods, 36000);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return goods;

	}

	@Override
	public boolean checkSell(GoodsQueryParam queryParam) {
		return goodsMapper.selectOneGoodsIsSell(queryParam) > 0;
	}

	@Override
	public SpkfkDesc getDescById(String spid) {
		final String key = REDIS_KEY_GOODS + ":" + spid + ":DESC";
		try {
			// 从缓存获取
			if(redisUtil.exists(key)) {
				SpkfkDesc spkfkDesc = (SpkfkDesc) redisUtil.get(key);
				return spkfkDesc;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		SpkfkDesc spkfkDesc = spkfkDescMapper.selectItemBySpid(spid);
		try {
			// 写入缓存
			if(spkfkDesc != null) {
				redisUtil.set(key, spkfkDesc, 36000);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return spkfkDesc;
	}

	@Override
	public List<SpitemRelation> getRelationById(GoodsQueryParam queryParam) {
		final String key = REDIS_KEY_GOODS + ":" + queryParam.getSpid() + "-" + queryParam.getSjid() + "-" + queryParam.getDwbh() + ":RELATION";
		try {
			// 从缓存获取
			if(redisUtil.exists(key)) {
				/*String jsonStr =  (String) redisUtil.get(key);
				List<SpitemRelation> spitemRelationList = JSONArray.parseArray(jsonStr, SpitemRelation.class);*/
				@SuppressWarnings("unchecked")
				List<SpitemRelation> spitemRelationList = (List<SpitemRelation>) redisUtil.get(key);
				return spitemRelationList;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<SpitemRelation> spitemRelationList = spitemRelationMapper.selectListBySpid(queryParam);
		try {
			// 写入缓存
			if(spitemRelationList != null && !spitemRelationList.isEmpty()) {
				redisUtil.set(key, spitemRelationList, 36000);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return spitemRelationList;
	}

	@Override
	public List<GoodsPre> getPreList(int sjid, String search, int page, int size) {
		int offset = (page - 1) * size;
		return goodsPreMapper.selectListByQueryParam(sjid, search, offset, size);
	}

	@Override
	public List<Goods> getPromotionList(GoodsQueryParam queryParam) {
		List<Goods> goodsList = goodsMapper.selectPromotionList(queryParam);
		for(int i = 0; i < goodsList.size(); i++) {
			String spid = goodsList.get(i).getSpid();
			int sjid = queryParam.getSjid();
			// 调用促销中心，获取该商品的促销信息
			Give give = giveInfoClient.goodsPromtionInfo(sjid, spid);
			/*if(give != null) {
				goodsList.get(i).setGive(give);
			}*/
		}
		return goodsList;
	}

	@Override
	public List<Goods> getListOnSell() {
		return goodsMapper.selectListOnSell();
	}
}
